package com.padakeji.android.pworks.module.image.view;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.app.PadaActivity;
import com.padakeji.android.pworks.content.IntentWrapper;
import com.padakeji.android.pworks.content.PadaResourceHelper;
import com.padakeji.android.pworks.module.image.common.ImageInfoUtil;
import com.padakeji.android.pworks.module.image.common.ImageSettings;
import com.padakeji.android.pworks.module.image.entity.ImageInfo;
import com.padakeji.android.pworks.ui.UIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AtanL on 2015/9/20.
 */
public class ImagePreviewActivity extends PadaActivity {

    public static final String EXTRA_IMAGE_INFOS = IntentWrapper.EXTRA_PREFIX + "IMAGE_INFOS";
    public static final String EXTRA_CURRENT_IMAGE_INDEX = IntentWrapper.EXTRA_PREFIX + "CURRENT_IMAGE_INDEX";
    public static final String EXTRA_MAX_SELECTED_COUNT = IntentWrapper.EXTRA_PREFIX + "MAX_SELECTED_COUNT";

    private ImageViewerFragment f;

    private List<ImageInfo> selectedImages;

    private List<ImageInfo> passedImageInfos;
    private int maxSelectedCount = ImageSettings.MAX_SELECTED_COUNT;

    private CheckBox selectBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pworks_activity_image_preview);

        selectedImages = ImagePickerActivity.getSelectedImages();

        selectBox = (CheckBox) findViewById(R.id.pworks_activity_image_preview_check_select);
        selectBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleCheck(getCurrentImageIndex());
            }
        });

        maxSelectedCount = getIntent().getIntExtra(EXTRA_MAX_SELECTED_COUNT, ImageSettings.MAX_SELECTED_COUNT);
        passedImageInfos = (ArrayList<ImageInfo>)getIntent().getSerializableExtra(EXTRA_IMAGE_INFOS);
        ArrayList<String> imagePaths = ImageInfoUtil.toPathArray(passedImageInfos);
        int passedIndex = getIntent().getIntExtra(EXTRA_CURRENT_IMAGE_INDEX, -1);

        f = (ImageViewerFragment)getSupportFragmentManager().findFragmentById(R.id.pworks_activity_image_preview_fragment_images);
        f.setOnImageChangeListener(new ImageViewerFragment.OnImageChangeListener() {
            @Override
            public void onImageSelected(int position) {
                checkItem(position);
            }
        });
        f.setArguments(imagePaths, passedIndex);
        checkItem(getCurrentImageIndex());
    }

    private int getCurrentImageIndex() {
        return f.getCurrentItemIndex();
    }

    private void checkItem(int itemIndex) {
        ImageInfo image = passedImageInfos.get(itemIndex);
        if (selectedImages.contains(image)) {
            selectBox.setChecked(true);
        } else {
            selectBox.setChecked(false);
        }
    }

    private void toggleCheck(int itemIndex) {
        ImageInfo imageInfo = passedImageInfos.get(itemIndex);
        if (selectedImages.contains(imageInfo)) {
            selectedImages.remove(imageInfo);
        } else {
            if (isSelectEnable()) {
                selectedImages.add(imageInfo);
            } else {
                new UIHelper(this).showToast(new PadaResourceHelper(this).getString(R.string.pworks_image_tips_max_select, maxSelectedCount));
            }
        }
        checkItem(itemIndex);

    }

    public boolean isSelectEnable() {
        return selectedImages.size() < maxSelectedCount ? true : false;
    }
}
